package com.zoweunion.mechanic.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.service.app.ScreenDataService;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 大屏数据
 * @author rentianfeng
 */
@RestController
@RequestMapping("/screenData")
@SuppressWarnings("unchecked")
public class ScreenDataController extends BaseController {
    @Autowired
    private ScreenDataService screenDataService;

    /**
     * 数据大屏
     * @return
     */
    @GetMapping("/getScreenData")
    public ResponseBean getScreenData(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        logBefore(logger,"数据大屏");

        Map<String, Object> reqMap = new HashMap<>();

        Map<String, Object> resultMap = new HashMap<>();

        String s_id = getCurrentUser(request).getS_id();
        reqMap.put("s_id",s_id);
        if (s_id.equals(Constants.DEMO_ACCOUNT_S_ID)) {

            reqMap.put("DEMO_ACCOUNT_S_ID",s_id);
        }
        getServerSid(s_id,reqMap);

        resultMap = screenDataService.getScreenData(reqMap);

        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
    }

    //厦工替换卡特服务商临时修改
    private void changeSid(Map<String, Object> params,HttpServletRequest request) throws MyException {
        if ( getCurrentUser(request).getS_id().equals("b2ee155eed7845809b24e84e54f35060")){
            params.put("s_id","6bd970ddd3e44e0f8646f951453ce18e");
        }
    }

    @RequestMapping(value = "/getScreenDataGPS")
    public ResponseBean getScreenDataGPS(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger,"数据大屏");

        int car_status = (int)(Math.random()*2+1);

        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",new HashMap<>());
    }

    /**
     * 获取全部车辆实时位置
     * @return
     */
    @RequestMapping(value = "/getRealTimeLocation")
    public ResponseBean getRealTimeLocation(@RequestParam(value = "s_id",required = false,defaultValue = "") String s_id ) throws MyException {
        logBefore(logger,"获取全部车辆实时位置");
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("s_id", s_id);
        List<Map<String, Object>> resultMap = screenDataService.getRealTimeLocation(reqMap);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
    }
    /**
     * 获取全部车辆前2条历史数据
     * @return
     */
    @RequestMapping(value = "/getRealTimeHistoryLocation")
    public ResponseBean getRealTimeHistoryLocation(@RequestParam(value = "s_id",required = false,defaultValue = "") String s_id ) throws MyException {
        logBefore(logger,"获取全部车辆历史数据");
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("s_id", s_id);
        List<Map<String, Object>> resultMap = screenDataService.getRealTimeHistoryLocation(reqMap);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
    }

    /**
     * 获取全部车辆实时位置
     * @return
     */
    @RequestMapping(value = "/getRealTimeLocationNew")
    public ResponseBean getRealTimeLocationNew(@RequestParam(value = "s_id",required = false,defaultValue = "") String s_id ) throws MyException {
        logBefore(logger,"获取全部车辆实时位置");
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("s_id", s_id);
        List<Map<String, Object>> resultMap = screenDataService.getRealTimeLocationNew(reqMap);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
    }

    /**
     * 获取全部车辆前2条历史数据
     * @return
     */
    @RequestMapping(value = "/getRealTimeHistoryLocationNew")
    public ResponseBean getRealTimeHistoryLocationNew(@RequestParam(value = "s_id",required = false,defaultValue = "") String s_id ) throws MyException {
        logBefore(logger,"获取全部车辆历史数据");
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("s_id", s_id);
        List<Map<String, Object>> resultMap = screenDataService.getRealTimeHistoryLocationNew(reqMap);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
    }

    /**
     * 获取全部车辆位置数据
     * @return
     */
    @RequestMapping(value = "/getRealTimeLocationAllCar")
    public List getRealTimeLocationAllCar() throws MyException {
        logBefore(logger,"获取全部车辆位置数据");
        return screenDataService.getRealTimeLocationAllCar();
    }
}
